import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import '../../../entity/record/football/zq_schedule_entity.dart';
import '../../../resources/colours.dart';
import '../../../utils/routes.dart';
import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_text.dart';

class ScheduleItem extends StatelessWidget {
  const ScheduleItem({super.key, required this.data});

  final ScheduleEntity? data;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          height: 36,
          width: Get.width,
          alignment: Alignment.center,
          child: QkdText(data!.roundName!,
            fontSize: 13,color: Colours.main,fontWeight: FontWeight.w500,
          )
        ),
        Column(
          children: List.generate(
            data?.schedules?.length ?? 0,
            (index) => ScheduleCell(
              index: index,
              schedules: data!.schedules![index],
              recordType: data?.recordType,
            )
          )
        ),
      ],
    );
  }
}

class ScheduleCell extends StatelessWidget {
  const ScheduleCell(
      {super.key,
      required this.schedules,
      required this.index,
      this.padding = 16,
      this.needColor = true,
      this.recordType});

  final int index;
  final double padding;
  final bool needColor;
  final int? recordType;
  final Schedules? schedules;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (schedules?.matchId != null) {
          Utils.onEvent("data_match_view",params: {"match_id": schedules?.matchId});
          Get.toNamed(
            recordType == 0?Routes.zqMatchDetail:Routes.lqMatchDetail,
            arguments: schedules?.matchId
          );
        }
        ;
      },
      child: Container(
        height: 48,
        padding: EdgeInsets.symmetric(horizontal: padding),
        color:
            index == 0 || index % 2 == 0 ? Colours.white : Colours.greyF5F7FA,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            QkdText(
              (schedules?.matchTime?.length ?? 0) > 11
                ? DateTime.tryParse(schedules?.matchTime ?? "")
                  ?.formatedString("MM-dd\nHH:mm")
                : (schedules?.matchTime ?? "").split(" ").join("\n"),
              fontSize: 12, color: Colours.grey66
            ),
            Expanded(
              flex: 3,
              child: QkdText(schedules?.leftName ?? '',
                  fontSize: 13,
                  maxLines: 2,
                  textAlign: TextAlign.end),
            ),
            Container(
              width: recordType == 1?60:42,
              alignment: Alignment.center,
              margin: const EdgeInsets.symmetric(horizontal: 8),
              child: DefaultTextStyle(
                style: const TextStyle(fontSize: 15,color: Colours.grey99),
                child: statusCheck()
              )
            ),
            Expanded(
              flex: 3,
              child: QkdText(schedules?.rightName ?? '',
                fontSize: 13,
                maxLines: 2,
                textAlign: TextAlign.start
              ),
            ),
            QkdText(
              (schedules?.matchTime?.length ?? 0) > 11 ?
              DateTime.tryParse(
                schedules?.matchTime ?? ""
              )?.formatedString("MM-dd\nHH:mm") :
              (schedules?.matchTime ?? "").split(" ").join("\n"),
              fontSize: 12, color: Colours.transparent
            )
          ],
        ),
      ),
    );
  }

  Widget scoreMore() {
    if (schedules?.leftScoreOT != null && schedules?.leftScorePK == null) {
      return QkdText('加时 ${schedules?.leftScoreOT}-${schedules?.rightScoreOT}',
        fontSize: 10,color: Colours.grey66,);
    } else if (schedules?.leftScoreOT == null && schedules?.leftScorePK != null) {
      return QkdText('点球 ${schedules?.leftScorePK}-${schedules?.rightScorePK}',
        fontSize: 10,color: Colours.grey66);
    } else if (schedules?.leftScoreOT != null && schedules?.leftScorePK != null) {
      return const QkdText('加｜点', fontSize: 10,color: Colours.grey66);
    } else {
      return Container();
    }
  }

  Color scoreColor() {
    int left = 0;
    int right = 0;
    if (schedules?.leftScoreEnd != null && schedules?.rightScoreEnd != null) {
      left = schedules!.leftScoreEnd! +
          (schedules?.leftScoreOT ?? 0) +
          (schedules?.leftScorePK ?? 0);
      right = schedules!.rightScoreEnd! +
          (schedules?.rightScoreOT ?? 0) +
          (schedules?.rightScorePK ?? 0);
    }
    if (left > right) {
      return Colours.redF13333;
    } else if (left < right) {
      return Colours.green;
    } else {
      return Colours.main;
    }
  }

  Widget statusCheck() {
    if(recordType == 0) {
      switch (schedules?.status) {
        case 7:
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (schedules?.leftScoreEnd != null)
              QkdText(
                '${schedules?.leftScoreEnd}-${schedules?.rightScoreEnd}',
                fontSize: 15,color: needColor ? scoreColor() : Colours.text,
                fontWeight: FontWeight.w500,
              ),
              scoreMore()
            ],
          );
        case 8:
          return const QkdText("取消");
        case 9:
          return const QkdText("中断");
        case 10:
          return const QkdText("腰斩");
        case 11:
          return const QkdText("延期");
        case 12:
          return const QkdText("待定");
        default:
          return const QkdText("vs");
      }
    }else{
      switch (schedules?.status) {
        case 10:
          return QkdText(
            '${schedules?.leftScoreEnd}-${schedules?.rightScoreEnd}',
            fontSize: 15,color: needColor ? scoreColor() : Colours.text,
            fontWeight: FontWeight.w500,
          );
        case 11:
          return const QkdText("取消");
        case 12:
          return const QkdText("中断");
        case 13:
          return const QkdText("腰斩");
        case 14:
          return const QkdText("延期");
        case 15:
          return const QkdText("待定");
        default:
          return const QkdText("vs");
      }
    }
  }
}
